﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;

//建造者模式
namespace Design
{
    /// <summary>
    /// FoodManager 类，即指导者
    /// </summary>
    public class FoodManager
    {
        public void Construct(Builder builder)
        {
            builder.BuildHamb();
            builder.BuildCoke();
            builder.BuildChip();
        }
    }

    /// <summary>
    /// Food 类，即产品类
    /// </summary>
    public class Food
    {
        Hashtable ht_food = new Hashtable();

        /// <summary>
        /// 添加食品
        /// </summary>
        /// <param name="strName">食品名称</param>
        /// <param name="price">食品价格</param>
        public void Add(string strName, string price)
        {
            ht_food.Add(strName, price);
        }


        /// <summary>
        /// 显示食品清单
        /// </summary>
        public void Show()
        {
            IDictionaryEnumerator myEnumerator = ht_food.GetEnumerator();
            Console.WriteLine("Food List:");
            Console.WriteLine("--------------------------------------");
            string strFoodList = "";
            while (myEnumerator.MoveNext())
            {
                strFoodList = strFoodList + "\n\n" + myEnumerator.Key.ToString();
                strFoodList = strFoodList + ":\t" + myEnumerator.Value.ToString();

            }
            Console.WriteLine(strFoodList);
            Console.WriteLine("\n--------------------------------------");
        }
    }

    /// <summary>
    /// Builder 类，抽象类 建造者
    /// </summary>
    public abstract class Builder
    {
        /// <summary>
        /// 添加汉堡
        /// </summary>
        public abstract void BuildHamb();

        /// <summary>
        /// 添加可乐
        /// </summary>
        public abstract void BuildCoke();

        /// <summary>
        /// 添加薯条
        /// </summary>
        public abstract void BuildChip();

        /// <summary>
        /// 返回结果
        /// </summary>
        /// <returns>食品对象</returns>
        public abstract Food GetFood();
    }


    /// <summary>
    /// NormalBuilder类：具体构造者，普通套餐
    /// </summary>
    public class NormalBuilder : Builder
    {
        private Food NormalFood = new Food();
        public override void BuildHamb()
        {
            NormalFood.Add("NormalHamb", "10.5");
        }

        public override void BuildCoke()
        {
            NormalFood.Add("CokeCole", "4.5");
        }

        public override void BuildChip()
        {
            NormalFood.Add("FireChips", "2");
        }

        public override Food GetFood()
        {
            return NormalFood;
        }
    }

    public class GlodBuilder : Builder
    {
        private Food GlodFood = new Food();

        public override void BuildHamb()
        {
            GlodFood.Add("GlodHamb", "13.5");
        }

        public override void BuildCoke()
        {
            GlodFood.Add("CokeCole", "4.5");
        }

        public override void BuildChip()
        {
            GlodFood.Add("FireChips", "3.5");
        }

        public override Food GetFood()
        {
            return GlodFood;
        }
    }


    public class BuilderClient
    {
        //public static void Main(string[] args)
        public void BuilderTest()
        {
            FoodManager foodmanager = new FoodManager();
            Builder instance;
            Console.WriteLine("Please Enter Food No");

            string No = Console.ReadLine();
            string assemblyString = ConfigurationSettings.AppSettings["KFC"].ToString();  //读取配置
            string foodType = ConfigurationSettings.AppSettings["No." + No];  //读取配置

            instance = (Builder)Assembly.Load(assemblyString).CreateInstance(assemblyString + "." + foodType);
            //instance = new NormalBuilder();   可根据配置或者直接new 对象

            foodmanager.Construct(instance);

            Food food = instance.GetFood();
            food.Show();
        }
    }
}
